#!/usr/bin/env python
# -*- coding: utf-8 -*-

import queue
from common.utils import LogUtil
from web_server.device_proxy import DeviceProxy
from web_server.http_service import HttpService
from web_server.mesg_service import MesgService
from web_server.poll_service import PollService
from web_server.perf_service import PerfService

logger = LogUtil.get_logger(__name__)

class ServiceMgr:
    def __init__(self):
        self.__event_queue = None
        self.__device_proxy = None
        self.__http_service = None
        self.__mesg_service = None
        self.__perf_service = None
        self.__poll_service = None
        try:
            self.__event_queue = queue.Queue()
            self.__device_proxy = DeviceProxy()
            self.__http_service = HttpService(self.__put_event, self.__device_proxy)
            self.__mesg_service = MesgService(self.__put_event, self.__device_proxy)
            self.__perf_service = PerfService(self.__put_event, self.__device_proxy)
            self.__poll_service = PollService(self.__put_event)
            self.__device_proxy.set_reload_cb(self.__mesg_service.put_mesg)
            self.__poll_service.set_del_services_cb(self.__device_proxy.handle_del_services)
            self.__poll_service.set_old_services_cb(self.__device_proxy.handle_old_services)
            self.__poll_service.set_new_services_cb(self.__device_proxy.handle_new_services)
        except Exception as e:
            logger.error(f"初始化服务失败: {e}")
            self.destroy()
            raise e

    def __del__(self):
        self.destroy()

    def __put_event(self, event: str):
        self.__event_queue.put(event)

    def destroy(self):
        if self.__poll_service is not None:
            self.__poll_service.destroy()
            self.__poll_service = None
        if self.__perf_service is not None:
            self.__perf_service.destroy()
            self.__perf_service = None
        if self.__mesg_service is not None:
            self.__mesg_service.destroy()
            self.__mesg_service = None
        if self.__http_service is not None:
            self.__http_service.destroy()
            self.__http_service = None
        
        if self.__device_proxy is not None:
            self.__device_proxy.destroy()
            self.__device_proxy = None

    def start(self):
        if not self.__http_service.start():
            logger.debug("http service start failed")
            return False
        logger.debug("http service start success")
        if not self.__perf_service.start():
            logger.debug("perf service start failed")
            return False
        logger.debug("perf service start success")
        if not self.__poll_service.start():
            logger.debug("poll service start failed")
            return False
        logger.debug("poll service start success")

        return True

    def wait(self):
        while True:
            event = self.__event_queue.get()
            match event:
                case "mesg":
                    logger.error("mesg service stopped")
                case "perf":
                    logger.error("perf service stopped")
                case "http":
                    logger.error("http service stopped")
                case "poll":
                    logger.error("poll service stopped")
                case _:
                    logger.error(f"unknown service event: {event}")
            break
        self.destroy()
